from typing import Literal, Optional, List

from pydantic import HttpUrl, computed_field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file="../.env", env_ignore_empty=True, extra="ignore", env_prefix="AW_"
    )
    API_V1_STR: str = ""
    ENVIRONMENT: Literal["dev", "prod"] = "dev"
    PROJECT_NAME: str
    SENTRY_DSN: Optional[HttpUrl] = None
    SENSITIVE_FIELD: List[str] = ["password", "token"]
    PRIVATE_REPO: Optional[str] = None

    # 注解的Key
    ANNOTATION_VALIDATE_KEY: str = "admission-webhook-demo.com/validate"
    ANNOTATION_MUTATE_KEY: str = "admission-webhook-demo.com/mutate"
    ANNOTATION_STATUS_KEY: str = "admission-webhook-demo.com/status"
    ANNOTATION_SIDECAR_IMAGE_KEY: str = "admission-webhook-demo.com/sidecar-image"
    ANNOTATION_SIDECAR_NAME_KEY: str = "admission-webhook-demo.com/sidecar-name"
    ANNOTATION_SIDECAR_COMMAND_KEY: str = "admission-webhook-demo.com/sidecar-command"
    IGNORE_NAMESPACES: List[str] = ["kube-system"]

    @computed_field(return_type=bool)  # type: ignore[prop-decorator]
    @property
    def debug(self):
        if self.ENVIRONMENT == "dev":
            return True
        return False


# 优先级
# 1.显式传递给类的参数（如 AppSettings(PRIVATE_REPO=xxx)）
# 2.环境变量（如 AW_PRIVATE_REPO=xxx）
# 3. .env 文件中的变量
# 4.类字段的默认值
# /app/core/config.py
settings = Settings(
    PROJECT_NAME="admission-webhook-demo-py",
    SENTRY_DSN=HttpUrl(
        "https://96e1bb9329070af647b0dbd9099f8f3b@o279472.ingest.us.sentry.io/4509868116148224"
    ),
)
